<?php

namespace app\modules\search\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\clinical\models\ExcelPd;

class SearchExcelPd extends ExcelPd
{
    public function rules()
    {
        // 只有在 rules() 函数中声明的字段才可以搜索
        return [
            [['ft_ill_time', 'ft_md_time', 'up_time', 'bf_time', 'lc_time', 'dn_time', 'sl_time'], 'safe'],
            [['ft_symptom', 'ft_process', 'else_ctnt'], 'string', 'max' => 255],
            [['ft_part', 'treatment', 'q_1','q_2','q_3','q_4','q_5','q_6','q_7','q_8','q_9','q_10','q_11','q_12','q_13','q_14','q_15','q_16','q_17','q_18','q_19','q_20','q_21','q_22','q_23','q_24','q_25','q_26','q_27','q_28','q_29','q_30'], 'integer']
        ];
    }

    public function search($params)
    {
        $query = ExcelPd::find()->joinWith(['crtinfo' => function($query) {
            $query->andWhere(['delete_at' => 0]);
        }]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'pk_id' => SORT_DESC,
                ]
            ],
        ]);

        // 从参数的数据中加载过滤条件，并验证
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $this->clearDate(['ft_ill_time', 'ft_md_time', 'up_time', 'bf_time', 'lc_time', 'dn_time', 'sl_time']);

        // 增加过滤条件来调整查询对象
        $query->andFilterWhere(['ft_ill_time' => $this->ft_ill_time])
              ->andFilterWhere(['ft_md_time' => $this->ft_md_time])
              ->andFilterWhere(['up_time' => $this->up_time])
              ->andFilterWhere(['bf_time' => $this->bf_time])
              ->andFilterWhere(['lc_time' => $this->lc_time])
              ->andFilterWhere(['dn_time' => $this->dn_time])
              ->andFilterWhere(['sl_time' => $this->sl_time])
              ->andFilterWhere(['ft_part' => $this->ft_part])
              ->andFilterWhere(['treatment' => $this->treatment])
              ->andFilterWhere(['q_1' => $this->q_1])
              ->andFilterWhere(['q_2' => $this->q_2])
              ->andFilterWhere(['q_3' => $this->q_3])
              ->andFilterWhere(['q_4' => $this->q_4])
              ->andFilterWhere(['q_5' => $this->q_5])
              ->andFilterWhere(['q_6' => $this->q_6])
              ->andFilterWhere(['q_7' => $this->q_7])
              ->andFilterWhere(['q_8' => $this->q_8])
              ->andFilterWhere(['q_9' => $this->q_9])
              ->andFilterWhere(['q_10' => $this->q_10])
              ->andFilterWhere(['q_11' => $this->q_11])
              ->andFilterWhere(['q_12' => $this->q_12])
              ->andFilterWhere(['q_13' => $this->q_13])
              ->andFilterWhere(['q_14' => $this->q_14])
              ->andFilterWhere(['q_15' => $this->q_15])
              ->andFilterWhere(['q_16' => $this->q_16])
              ->andFilterWhere(['q_17' => $this->q_17])
              ->andFilterWhere(['q_18' => $this->q_18])
              ->andFilterWhere(['q_19' => $this->q_19])
              ->andFilterWhere(['q_20' => $this->q_20])
              ->andFilterWhere(['q_21' => $this->q_21])
              ->andFilterWhere(['q_22' => $this->q_22])
              ->andFilterWhere(['q_23' => $this->q_23])
              ->andFilterWhere(['q_24' => $this->q_24])
              ->andFilterWhere(['q_25' => $this->q_25])
              ->andFilterWhere(['q_26' => $this->q_26])
              ->andFilterWhere(['q_27' => $this->q_27])
              ->andFilterWhere(['q_28' => $this->q_28])
              ->andFilterWhere(['q_29' => $this->q_29])
              ->andFilterWhere(['q_30' => $this->q_30])
              ->andFilterWhere(['like', 'ft_symptom', $this->ft_symptom])
              ->andFilterWhere(['like', 'ft_process', $this->ft_process])
              ->andFilterWhere(['like', 'else_ctnt', $this->else_ctnt]);

        return $dataProvider;
    }

    protected function clearDate($arr)
    {
        foreach ($arr as $param) {
            $this->$param = preg_replace('/_*(-|:)_*(-|:)_*/', '', $this->$param);
            $this->$param = preg_replace('/_+:_+/', '', $this->$param);
        }
    }
}